package com.flashphoner.api
{
	import com.flashphoner.Logger;
	import com.flashphoner.api.data.PhoneConfig;
	import com.flashphoner.api.management.SoundControl;
	import com.flashphoner.api.management.VideoControl;
	
	import flash.events.AsyncErrorEvent;
	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.net.Responder;

	public class CallServerProxy
	{
		private var outStream:NetStream;
		
		private var nc:NetConnection;
		private var flashCall:Call;
		
		private var sendVideo:Boolean = false;
		
		public function CallServerProxy(call:Call,nc:NetConnection)
		{		
			this.nc = nc;
			this.flashCall = call;
		}
		
		public function sendDtmf(dtmf:String):void{
			Logger.info("PhoneServerProxy.sendDtmf() "+dtmf);
			nc.call("sendDtmf",null,dtmf,flashCall.id);
		}
		
		public function hangup():void{
			Logger.info("PhoneServerProxy.hangup() call.id: "+ flashCall.id);
			nc.call("hangup",null,flashCall.id);
		}
		
		public function transfer(callee:String):void{
			Logger.info("PhoneServerProxy.transfer() call.id: "+flashCall.id+";callee: "+callee);
			nc.call("transfer",null,flashCall.id,callee);
		}		
		
		public function hold(isHold:Boolean):void{
			Logger.info("PhoneServerProxy.setStatusHold() call.id: "+flashCall.id+";isHold: "+isHold);
			nc.call("hold",null,flashCall.id,isHold);
		}		
		
		public function answer(isVideoCall:Boolean):void{
			Logger.info("PhoneServerProxy.answer() call.id: "+flashCall.id);
			nc.call("answer",null,flashCall.id,isVideoCall);	
		}
		
		public function publish(login:String):void{
			Logger.info("PhoneServerProxy.publish() call.id: "+flashCall.id);			
			if (outStream == null){
				outStream = new NetStream(nc);
				outStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncErrorHandler);
				outStream.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus);									
				outStream.attachAudio(SoundControl.getInstance().getMicrophone());		
				
				if (PhoneConfig.VIDEO_ENABLED && sendVideo){	 
					outStream.attachCamera(VideoControl.getInstance().getCam());					 
				}		
				outStream.publish(login+"_"+flashCall.id);
			}					
		}
		
		public function unpublish():void{			
			if (outStream != null){				
				outStream.removeEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncErrorHandler);
				outStream.removeEventListener(NetStatusEvent.NET_STATUS,onNetStatus);		
				outStream.close();
				outStream=null;
			}		
		}		
		public function isSendVideo(flag:Boolean):void{
			sendVideo = flag;
			if (outStream == null){
				return;
			}
			if (sendVideo && flashCall != null && !flashCall.isVideoCall){
				nc.call("updateCallToVideo",null,flashCall.id);
			}
			if (PhoneConfig.VIDEO_ENABLED && sendVideo){
				outStream.attachCamera(VideoControl.getInstance().getCam());
			}
			if (!sendVideo){
				outStream.attachCamera(null);
			}		
		} 
		
		private function asyncErrorHandler(event: AsyncErrorEvent):void {
		}
		
		private function onNetStatus(event : NetStatusEvent) : void{
		}
	}
}